﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Networks;
using DiscoveryLogic.Analytics.DataObjects.Visuals;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.Modules.VisualSettings
{
    /// <summary>
    /// Interaction logic for EdgeVisualSetting.xaml
    /// </summary>
    public partial class EdgeVisualSetting : UserControl
    {
        public event VerticeVisualSetting.OnNetworkVisualUpdated NetworkVisualUpdated;
        private NetworkVisual _NetworkVisual;

        public EdgeVisualSetting(string networkName)
        {
            InitializeComponent();
            this._NetworkVisual = NetworkVisual.Read(networkName);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            List<EntityType> entityTypes=new List<EntityType>();
            this.cboEntityTo.Items.Clear();
            this.cboEntityFrom.Items.Clear();
            Network network = Network.Read(this._NetworkVisual.NetworkName);
            foreach(EntityBase entity in network.Records)
            {
                if(!entityTypes.Contains(entity.EntityType))
                    entityTypes.Add(entity.EntityType);
            }
            foreach(EntityType entityType in entityTypes)
            {
                this.cboEntityTo.Items.Add(entityType.ToString());
                this.cboEntityFrom.Items.Add(entityType.ToString());
            }
            this.cboEdgeThickMeasure.Items.Clear();
            foreach(EdgeThicknessMeasure thickMeasure in Enum.GetValues(typeof(EdgeThicknessMeasure)))
            {
                this.cboEdgeThickMeasure.Items.Add(thickMeasure.ToString());
            }
            this.cboLineStyle.Items.Clear();
            foreach(EdgeLineStyle lineStyle in Enum.GetValues(typeof(EdgeLineStyle)))
            {
                this.cboLineStyle.Items.Add(lineStyle.ToString());
            }

            if (this.cboEntityFrom.Items.Count > 0)
                this.cboEntityFrom.SelectedIndex = 0;
            if (this.cboEntityTo.Items.Count > 0)
                this.cboEntityTo.SelectedIndex = 0;
        }

        private void cboEntityFrom_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cboEntityFrom.SelectedIndex >= 0 &&
                this.cboEntityTo.SelectedIndex >= 0)
            {
                EntityType fromEntity =
                    (EntityType) Enum.Parse(typeof (EntityType), this.cboEntityFrom.SelectedItem.ToString());
                EntityType toEntity =
                    (EntityType) Enum.Parse(typeof (EntityType), this.cboEntityTo.SelectedItem.ToString());
                EdgeVisual ev = this._NetworkVisual.EdgeVisuals[fromEntity][toEntity];
                this.DisplayEdgeVisual(ev);
            }
        }

        private void cboEntityTo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cboEntityFrom.SelectedIndex >= 0 &&
                this.cboEntityTo.SelectedIndex >= 0)
            {
                EntityType fromEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityFrom.SelectedItem.ToString());
                EntityType toEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityTo.SelectedItem.ToString());
                EdgeVisual ev = this._NetworkVisual.EdgeVisuals[fromEntity][toEntity];
                this.DisplayEdgeVisual(ev);
            }    
        }

        private void DisplayEdgeVisual(EdgeVisual ev)
        {
            this.canvasEdge.Background = new SolidColorBrush(ev.Color);

            this.cboLineStyle.Text = ev.LineStyle.ToString();
            this.sliderThickness.Value = ev.Thickness;
            this.cboEdgeThickMeasure.Text = ev.ThicknessMeasure.ToString();
            if(ev.ThicknessMeasure==EdgeThicknessMeasure.Fixed)
            {
                this.rdFixed.IsChecked = true;
                this.rdDynamic.IsChecked = false;
            }
            else
            {
                this.rdDynamic.IsChecked = true;
                this.rdFixed.IsChecked = false;
            }
            
            this.ckShowDistance.IsChecked = ev.ShowDistance;
            this.sliderDistanceScale.Value = ev.DistanceScale;

            
        }

        private void btnColor_Click(object sender, RoutedEventArgs e)
        {
            if(this.cboEntityFrom.SelectedIndex >=0 && 
                this.cboEntityTo.SelectedIndex>=0)
            {
                EntityType fromEntity =
                    (EntityType) Enum.Parse(typeof (EntityType), this.cboEntityFrom.SelectedItem.ToString());
                EntityType toEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityTo.SelectedItem.ToString());
                Microsoft.Samples.CustomControls.ColorPickerDialog cPicker
                    = new Microsoft.Samples.CustomControls.ColorPickerDialog();
                EdgeVisual ev = this._NetworkVisual.EdgeVisuals[fromEntity][toEntity];
                
                cPicker.StartingColor = ev.Color;
                bool? dialogResult = cPicker.ShowDialog();
                if (dialogResult != null && (bool)dialogResult)
                {
                    ev.Color = cPicker.SelectedColor;
                    this.DisplayEdgeVisual(ev);
                }
            }
            
        }

        private void rdFixed_Click(object sender, RoutedEventArgs e)
        {
            if (!this.IsInitialized)
                return;

            if (this.cboEntityFrom.SelectedIndex >= 0 &&
                this.cboEntityTo.SelectedIndex >= 0)
            {
                EntityType fromEntity =
                    (EntityType) Enum.Parse(typeof (EntityType), this.cboEntityFrom.SelectedItem.ToString());
                EntityType toEntity =
                    (EntityType) Enum.Parse(typeof (EntityType), this.cboEntityTo.SelectedItem.ToString());
                EdgeVisual ev = this._NetworkVisual.EdgeVisuals[fromEntity][toEntity];
                
                if (rdFixed.IsChecked.Value)
                {
                    ev.ThicknessMeasure = EdgeThicknessMeasure.Fixed;
                    rdDynamic.IsChecked = false;
                    this.sliderThickness.Visibility = Visibility.Visible;
                    this.cboEdgeThickMeasure.Visibility = Visibility.Hidden;
                }
                else
                {
                    rdDynamic.IsChecked = true;
                    this.sliderThickness.Visibility = Visibility.Hidden;
                    this.cboEdgeThickMeasure.Visibility = Visibility.Visible;
                    if (this.cboEdgeThickMeasure.SelectedIndex >= 0)
                        ev.ThicknessMeasure =
                            (EdgeThicknessMeasure)
                            Enum.Parse(typeof(EdgeThicknessMeasure), this.cboEdgeThickMeasure.SelectedItem.ToString());
                }
            }
        }

        private void rdDynamic_Click(object sender, RoutedEventArgs e)
        {
            if (!this.IsInitialized)
                return;

            if (this.cboEntityFrom.SelectedIndex >= 0 &&
                this.cboEntityTo.SelectedIndex >= 0)
            {
                EntityType fromEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityFrom.SelectedItem.ToString());
                EntityType toEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityTo.SelectedItem.ToString());
                EdgeVisual ev = this._NetworkVisual.EdgeVisuals[fromEntity][toEntity];
                
                if (this.rdDynamic.IsChecked.Value)
                {
                    rdFixed.IsChecked = false;
                    this.sliderThickness.Visibility = Visibility.Hidden;
                    this.cboEdgeThickMeasure.Visibility = Visibility.Visible;
                    if (this.cboEdgeThickMeasure.SelectedIndex >= 0)
                        ev.ThicknessMeasure =
                            (EdgeThicknessMeasure)
                            Enum.Parse(typeof(EdgeThicknessMeasure), this.cboEdgeThickMeasure.SelectedItem.ToString());
                }
                else
                {
                    ev.ThicknessMeasure = EdgeThicknessMeasure.Fixed;
                    rdFixed.IsChecked = true;
                    this.sliderThickness.Visibility = Visibility.Visible;
                    this.cboEdgeThickMeasure.Visibility = Visibility.Hidden;
                }
            }
        }

        private void sliderThickness_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if(!this.IsInitialized)
                return;

            this.lblThickness.Content = Math.Round(this.sliderThickness.Value, 1).ToString();
            if (this.cboEntityFrom.SelectedIndex >= 0 &&
                this.cboEntityTo.SelectedIndex >= 0)
            {
                EntityType fromEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityFrom.SelectedItem.ToString());
                EntityType toEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityTo.SelectedItem.ToString());
                EdgeVisual ev = this._NetworkVisual.EdgeVisuals[fromEntity][toEntity];
                
                ev.Thickness = sliderThickness.Value;
            } 
        }

        private void cboEdgeThickMeasure_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cboEntityFrom.SelectedIndex >= 0 &&
                this.cboEntityTo.SelectedIndex >= 0)
            {
                EntityType fromEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityFrom.SelectedItem.ToString());
                EntityType toEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityTo.SelectedItem.ToString());
                EdgeVisual ev = this._NetworkVisual.EdgeVisuals[fromEntity][toEntity];
                
                if (this.cboEdgeThickMeasure.SelectedIndex >= 0)
                    ev.ThicknessMeasure =
                        (EdgeThicknessMeasure) Enum.Parse(typeof (EdgeThicknessMeasure), this.cboEdgeThickMeasure.SelectedItem.ToString());
            } 
        }

        private void btnApply_Click(object sender, RoutedEventArgs e)
        {
            this._NetworkVisual.Save();
            if (this.NetworkVisualUpdated != null)
                this.NetworkVisualUpdated(this._NetworkVisual);

            if(this.Parent is Window)
            {
                ((Window)this.Parent).Close();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if (this.Parent is Window)
            {
                ((Window)this.Parent).Close();
            }
        }

        private void ckShowDistance_Click(object sender, RoutedEventArgs e)
        {
            if (this.cboEntityFrom.SelectedIndex >= 0 &&
                this.cboEntityTo.SelectedIndex >= 0)
            {
                EntityType fromEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityFrom.SelectedItem.ToString());
                EntityType toEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityTo.SelectedItem.ToString());
                EdgeVisual ev = this._NetworkVisual.EdgeVisuals[fromEntity][toEntity];
                
                ev.ShowDistance = this.ckShowDistance.IsChecked.Value;
            }
        }

        private void cboLineStyle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cboEntityFrom.SelectedIndex >= 0 &&
                this.cboEntityTo.SelectedIndex >= 0)
            {
                EntityType fromEntity =
                    (EntityType) Enum.Parse(typeof (EntityType), this.cboEntityFrom.SelectedItem.ToString());
                EntityType toEntity =
                    (EntityType) Enum.Parse(typeof (EntityType), this.cboEntityTo.SelectedItem.ToString());
                EdgeVisual ev = this._NetworkVisual.EdgeVisuals[fromEntity][toEntity];
                
                if (this.cboLineStyle.SelectedIndex >= 0)
                    ev.LineStyle = (EdgeLineStyle) Enum.Parse(typeof (EdgeLineStyle), this.cboLineStyle.SelectedItem.ToString());
            }
        }

        private void sliderDistanceScale_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if(!this.IsInitialized)
                return;

            this.lblDistanceScale.Content = Math.Round(this.sliderDistanceScale.Value, 1).ToString();
            if (this.cboEntityFrom.SelectedIndex >= 0 &&
                this.cboEntityTo.SelectedIndex >= 0)
            {
                EntityType fromEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityFrom.SelectedItem.ToString());
                EntityType toEntity =
                    (EntityType)Enum.Parse(typeof(EntityType), this.cboEntityTo.SelectedItem.ToString());
                EdgeVisual ev = this._NetworkVisual.EdgeVisuals[fromEntity][toEntity];
                
                ev.DistanceScale = this.sliderDistanceScale.Value;
            }
        }
    }
}
